﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Web.Mvc;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;
using Newtonsoft.Json;

namespace GWEB.WebFrame.Course
{
    public class BaseCourseDetailController : PageInfoBase
    {
        [ActionFilter(IsLogin = false)]
        public ActionResult Index(String Id, String KeyCode)
        {
            ViewBag.Id = Id;
            ViewBag.KeyCode = KeyCode;
            return View(BasePage.V("Course/BaseCourseDetail"));
        }
        [ActionFilter(IsLogin = false)]
        [HttpPost]
        public JsonResult AddClass(String v, String id, String keyCode)
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Code("0001", "您没有权限"));
            DataRow drUser = BasePage.GetUser(Session);
            var Course = JsonConvert.DeserializeObject<cu_Course>(v);
            String sql = @"SELECT * FROM cu_Course where CourseName= '" + Course.CourseName + "'";
            BasePage.bd.ColumnNameToLower = false;
            DataTable dt = BasePage.bd.ExecuteTable(sql);
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    return BasePage.Json(ResultData.Code("0009", "已存在"));
                }
                else
                {
                    String sqls = @"INSERT INTO cu_Course (ClassId,CourseName,StuNumber,Dperiod,StartMonth,EndMonth,IsOrder,IsTentative,MakeNumber,AuditionsNumber,Remark,state,LeagueId,CreateId,Createdate)
                                 VALUES ('" + Course.ClassId + "','" + Course.CourseName + "','" + Course.StuNumber + "','" + Course.Dperiod + "','" + Course.StartMonth + "','" + Course.EndMonth + "','" + Course.IsOrder + "','" + Course.IsTentative + "'" +
                                 ",'" + Course.MakeNumber + "','" + Course.AuditionsNumber + "','" + Course.Remark + "','" + Course.state + "','" + Fun.cString(drUser["LeagueId"].ToString()) + "','" + BasePage.GetUserId(Session) + "','" + DateTime.Now + "')";
                    BasePage.bd.ColumnNameToLower = false;
                    dt = BasePage.bd.ExecuteTable(sqls);
                }
            }
            String str = JsonConvert.SerializeObject(dt);
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));
        }

        [ActionFilter(IsLogin = false)]
        [HttpPost]
        public JsonResult UpdateClass(String v, String id, String keyCode)
        {
            if (!Fun.IDVerify(id, keyCode))
                return BasePage.Json(ResultData.Code("0001", "您没有权限"));
            var Course = JsonConvert.DeserializeObject<cu_Course>(v);
            String sql = @"UPDATE cu_Course SET CourseName = '" + Course.CourseName + "',StuNumber = '" + Course.StuNumber + "',Dperiod = '" + Course.Dperiod + "'" +
                ",StartMonth = '" + Course.StartMonth + "',EndMonth = '" + Course.EndMonth + "',IsOrder = '" + Course.IsOrder + "',IsTentative = '" + Course.IsTentative + "'" +
                ",MakeNumber = '" + Course.MakeNumber + "',AuditionsNumber = '" + Course.AuditionsNumber + "',Remark = '" + Course.Remark + "',state = '" + Course.state + "' WHERE id = '" + Course.id + "'";
            BasePage.bd.ColumnNameToLower = false;
            DataTable dt = BasePage.bd.ExecuteTable(sql);
            String str = JsonConvert.SerializeObject(dt);
            BasePage.bd.ColumnNameToLower = true;
            List<Dictionary<String, Object>> data = JsonConvert.DeserializeObject<List<Dictionary<String, Object>>>(str);
            return BasePage.Json(ResultData.OK(data));
        }

        protected override void FieldAddInit(DataRow data)
        {
            data["CreateId"] = BasePage.GetUserId(Session);
            DataRow drUser = BasePage.GetUser(Session);
            data["LeagueId"] = Fun.cString(drUser["LeagueId"].ToString());
            ViewBag.Lx = 0;
            data["state"] = 1;
            data["IsOrder"] = 1;
            data["IsTentative"] = 0;
            data["Createdate"] = DateTime.Now.ToString();
            base.FieldAddInit(data);
        }
        protected override void FieldEditInit(DataRow data)
        {
            ViewBag.Lx = 1;
            base.FieldEditInit(data);
        }
    }
}
public class cu_Course
{
    public int? id;
    public int? ClassId;
    public string CourseName;
    public int? StuNumber;
    public int? Dperiod;
    public int? StartMonth;
    public int? EndMonth;
    public int? IsOrder;
    public int? IsTentative;
    public int? MakeNumber;
    public int? AuditionsNumber;
    public string Remark;
    public int? state;
    public int? LeagueId;
    public int? CreateId;
    public DateTime? Createdate;

}
